﻿#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WpfFootball.BingSearchService;
using System.Web.Services.Protocols;
using System.Net;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Windows;
using System.Windows.Resources;
#endregion

namespace WpfFootball.Media
{
    /// <summary>
    /// Contains methods to generate random images by using the Bing search service.
    /// </summary>
    public class ImageGenerator
    {
        #region Fields
        private string appID = "F095B2CD36EB025EE92E5E010653451D0502B158";
        private BingService service;
        #endregion

        public ImageGenerator()
        {
            service = new BingService();
        }

        /// <summary>
        /// Builds an image request.
        /// </summary>
        /// <param name="query">A string containing the search word.</param>
        /// <returns>A SearchRequest that contains the fully image request.</returns>
        private SearchRequest BuildImageSearchRequest(string query)
        {
            SearchRequest request = new SearchRequest();

            request.AppId = appID;
            request.Query = query;
            request.Sources = new SourceType[] { SourceType.Image };
            request.Version = "2.0";
            request.Adult = AdultOption.Moderate;
            request.AdultSpecified = true;

            request.Image = new ImageRequest();
            request.Image.Count = 10;
            request.Image.CountSpecified = true;
            request.Image.Offset = 0;
            request.Image.OffsetSpecified = true;

            return request;
        }

        /// <summary>
        /// Uses the contents of a ImageResult from a SearchResponce object 
        /// to create and load an image.
        /// </summary>
        /// <param name="response">An object containg the url and another info about the picture to load.</param>
        /// <returns>A BitImage object that represent the image from the specified ImageResult.</returns>
        private BitmapImage CreateImgFromImageResult(ImageResult response)
        {
            Image image = new Image();
            BitmapImage bmImage = new BitmapImage();
            bmImage.BeginInit();
            bmImage.UriSource = new Uri(response.MediaUrl);
            bmImage.EndInit();

            do
            {
                System.Windows.Forms.Application.DoEvents();
            } while (bmImage.IsDownloading);

            return bmImage;
        }

        /// <summary>
        /// Creates a random image by building a search request for a team.
        /// </summary>
        /// <param name="TeamName">The name of the team to generate the image.</param>
        public void GetRandomTeamImage(string TeamName)
        {
            try
            {
                SearchRequest request = BuildImageSearchRequest(TeamName);
                SearchResponse response = service.Search(request);
                BitmapImage image = CreateImgFromImageResult(response.Image.Results[0]);
            }
            catch (SoapException ex)
            {
                Console.WriteLine(ex.Detail);
            }
            catch (WebException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}
